<?php

namespace app\admin\controller;

use app\common\business\AdminRole;
use app\common\lib\Arr;
use app\common\lib\Param;
use app\common\lib\Show;
use think\response\Json;

class Notice extends AdminBase
{
    protected array $authData = [];

    public function initialize()
    {
        parent::initialize(); // 调用父类的initialize方法，如果有的话

        $this->authData = [
            'area_id' => $this->adminAreaId,
            'admin_id' => $this->adminId,
            'roles' => $this->adminRoleId
        ];
    }

    /**
     * 管理后台列表
     * @return Json
     */
    public function list(): Json
    {
        list($keyword, $page,$limit,$categoryId) = Param::getMore([
            ['keyword', ''],
            ['page', null],
            ['limit', null],
            ['category_id', []],
        ], $this->request, true);

        $noticeBis = new \app\common\business\Notice();

        try {
            $result=$noticeBis->getTableList($page,$limit,$keyword,$categoryId,$this->authData);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        $result['list'] = $noticeBis->resetInfo($result['list']);
        return Show::success($result);
    }

    /**
     * 新增/保存数据
     * @return Json
     */
    public function save(): Json
    {
        list($id, $status, $title, $description, $sort, $category_id, $face_url, $origin, $content, $is_hot, $area_id) = Param::postMore([
            ['id',null],
            ['status', 1],
            ['title',''],
            ['description', ''],
            ['sort', 0],
            ['category_id', 0],
            ['face_url', ''],
            ['origin', ''],
            ['content', ''],
            ['is_hot', 2],
            ['area_id', 0],
        ], $this->request, true);

        $data=[
            'id'=>$id,
            'status'=>$status,
            'title'=>$title,
            'description'=>$description,
            'sort'=>$sort,
            'category_id'=>$category_id,
            'face_url'=>$face_url,
            'origin'=>$origin,
            'content'=>$content,
            'is_hot'=>$is_hot,
            'area_id'=>$area_id,
        ];
        $validate = new \app\admin\validate\Notice();
        $scene=empty($data['id'])?'create':'update';
        if (!$validate->scene($scene)->check($data)) {
            return Show::error([], $validate->getError());
        }
        try {
            $result = (new \app\common\business\Notice())->save($data,$this->authData);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }

    /**
     * 修改状态
     * @return Json
     */
    public function updateStatus(): Json
    {
        list($id, $status) = Param::postMore([
            ['id',null],
            ['status', null],
        ], $this->request, true);

        $data=[
            'id'=>$id,
            'status'=>$status,
        ];
        try {
            $result = (new \app\common\business\Notice())->updateStatus($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }

    /**
     * 根据id删除
     * @return Json
     */
    public function deleteById(): Json
    {
        $id=$this->request->get('id');
        try {
            $result=(new \app\common\business\Notice())->deleteById($id);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "删除成功");
        }
        return Show::error([], '删除失败');
    }
}
